function createTableRow(dataArray) {
	var tr = $('<tr/>');
	for(var i = 0, l = dataArray.length; i < l; i++) {
		var td = $('<td/>');
		td.text(dataArray[i]);
		tr.append(td);
	}
	return tr;
}

function createTableRow2(dataArray) {
	var tr = $('<tr/>');
	for(var i = 0, l = dataArray.length; i <= l; i++) {
		var td = $('<td/>');
		if(i == l) {
			var supplier = dataArray[0];
			var detailBtn =  $('<a/>', {
				'text': '详细',
				'href':'javascript:getBackDetail("' + escape(supplier) + '")',
			});
			detailBtn.click(function() {

			});
			td.append(detailBtn);
		} else {
			td.text(dataArray[i]);
		}
		tr.append(td);
	}
	return tr;
}

function createTableRow3(dataArray) {
	var tr = $('<tr/>');
	for(var i = 0, l = dataArray.length; i < l; i++) {
		var td = $('<td/>');
		if(i == 10 || i == 11 || i == 13) {
			td.text(dataArray[i] == '1'? '是' : '否');
		} else {
			td.text(dataArray[i]);
		};
		tr.append(td);
	}
	return tr;
}

function getBackDetail(query) {
	$('#back-detail-table tbody').empty();
	$.get('/software/get_back_detail', query, function(result){
		for(var i = 0, l = result.data.length; i < l; i++) {
			var tr = createTableRow3(result.data[i]);
			$('#back-detail-table tbody').append(tr);
		}
		$('#back-detail-table').tablesorter();
	}, 'json');
}

function getTableFuncs() {
	var drawTable = {
		finish: function() {
			$('#finished-table tbody').empty();
			$('#unfinished-table tbody').empty();
			$('#table-head').text('当前线网软件版本');
			$.get('/software/get_finished_data', function(result) {
				for (var i = 0, l = result.data.length; i < l; i++) {
					var tr = createTableRow(result.data[i]);
					$('#finished-table tbody').append(tr);
				}
				$('#finished-table').tablesorter();
			}, 'json');
			$.get('/software/get_unfinished_data', function(result) {
				for (var i = 0, l = result.data.length; i < l; i++) {
					var tr = createTableRow(result.data[i]);
					$('#unfinished-table tbody').append(tr);
				}
				$('#unfinished-table').tablesorter();
			}, 'json');
		},
		back: function() {
			$('#back-table tbody').empty();
			$('#table-head').text('软件升级统计');
			$.get('/software/get_back_data', function(result) {
				for (var i = 0, l = result.data.length; i < l; i++) {
					var tr = createTableRow(result.data[i]);
					$('#back-table tbody').append(tr);
				}
				$('#back-table').tablesorter();
			}, 'json');
		},
		downloadtime: function() {
			$('#downloadtime-table tbody').empty();
			$('#table-head').text('软件完成下载花费时间');
			$.get('/software/get_downloadtime_data', function(result) {
				for (var i = 0, l = result.data.length; i < l; i++) {
					var tr = createTableRow(result.data[i]);
					$('#downloadtime-table tbody').append(tr);
				}
				$('#downloadtime-table').tablesorter();
			}, 'json');
		}
	}

	return drawTable;
}

function getFilters(mark) {
	var filters = $('.filter-option-' + mark);
	var oQuery = {};
	for(var i = 0, l = filters.length; i < l; i++){
		if ($(filters[i]).val() != '') {
			oQuery[$(filters[i]).data('filter')] = $(filters[i]).val();
		};
	}
	return oQuery;
}

$(function() {
	$('.panel-tab').hide();
	$('#left-nav').find('.list-group-item').click(function() {
		var tab = $(this).data('tab');
		var drawTables = getTableFuncs();
		$(this).addClass('active').siblings().removeClass('active');
		$('.panel-tab').hide();
		$('#' + tab + '-tab').show();
		drawTables[tab]();
	});

	$('.filter-submit').click(function() {
		var sort = $(this).data('sort');
		var oQuery = getFilters(sort);
		var table = $('#' + sort + '-table tbody');
		var url = '/software/get_' + sort + '_data';
		table.empty();
		$.get(url, oQuery, function(result){
			for(var i = 0, l = result.data.length; i < l; i++) {
				var tr = createTableRow(result.data[i]);
				table.append(tr);
			}
		}, 'json');
		if (sort == 'back') {
			getBackDetail(oQuery);
		};
	});

	$('.date-input').datepicker({
		changeMonth: true,
		changeYear: true,
		constrainInput: true,
		dateFormat: "yy-mm-dd",
		duration: 'fast',
		hideIfNoPrevNext: true,
		monthNamesShort: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
		dayNamesMin: ['日', '一', '二', '三', '四', '五', '六'],
		selectOtherMonths: true,
		showAnim: 'fadeIn',
		showOtherMonths: true,
		showWeek: false,
		yearSuffix: ''
	});

	theadFixed($('#finished-table'));
	theadFixed($('#unfinished-table'));
	theadFixed($('#back-table'));
	theadFixed($('#back-detail-table'));
	theadFixed($('#downloadtime-table'));
});